﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Castle.Core;
using Castle.MicroKernel.Registration;
using MiragePWF.Common.Commons;
using MiragePWF.Common.Container;
using MiragePWF.Common.Messages;
using MiragePWF.Common.Messages.XML;
using MiragePWF.Common.Utilities;
using MiragePWF.NH.UOW;
using Wolfut.MirageEE.Domain.CommonUtilities;
using Wolfut.MirageEE.UI.UICommonUtilities;

namespace Wolfut.MirageEE.Client
{
    
    public class AppStart
    {
        [STAThread]
        public static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            ConfigureIOC();
           //Start UnitOfWork
            UnitOfWork.Start();
            Application.Run(IOC.Resolve<MainForm1>());
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            HandleException(null,"");
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            HandleException(e.Exception , "");
        }
        public static void HandleException(Exception ex, string policy)
        {
           #if DEBUG
              MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            #endif
             #if !DEBUG
            MessageBox.Show("错误已经被记录，请联系系统管理员。","错误", MessageBoxButtons.OK, MessageBoxIcon.Stop);
#endif
        }

        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            UnitOfWork.Current.Dispose();
        }
        private static void ConfigureIOC()
        {
           ;
            IOC.Initialize();
            ConfigIOC();
            ConfigureFormIOC();
            //inject proxy or remoting
            ConfigProxyOrRemotingIOC();
            ConfigRepositoryIOC();
            ;
        }
        private static void ConfigIOC()
        {
            //Message provider
            
            IOC.Container.Register(
                Component.For<IMessageProvider>()
                                       .ImplementedBy<ConfigMessageProvider>()
                                       .Parameters(Parameter.ForKey("path").Eq(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Constant.Message.MessageFileName)))
                                       );
            //exception handling

            IOC.Container.AddComponent("ExceptionHandler", typeof(ClientExceptionHandler), typeof(WinClientExceptionHandler));
  
        }
        private static void ConfigureFormIOC()
        {
            Assembly assembly = ReflectUtil.ObtainAssembly(Constant.UIRelectionConstant.UIAssembly);
            //inject forms
            IOC.Container.AddComponent("main.form", typeof(MainForm1));
            //系统参数设置
            IOC.Container.AddComponentLifeStyle("SysParamForm",
                                        assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".SysParamForm"), LifestyleType.Transient);

           //币别            
            IOC.Container.AddComponentLifeStyle("CurrencyCategoryForm",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".CurrencyCategoryForm"), LifestyleType.Transient);

            //CodeRuleForm
            IOC.Container.AddComponentLifeStyle("CodeRuleForm",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".CodeRuleForm"), LifestyleType.Transient);
            
            IOC.Container.AddComponentLifeStyle("BillCodeRuleForm",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".BillCodeRuleForm"), LifestyleType.Transient);
            //部门档案
            IOC.Container.AddComponentLifeStyle("DepartmentForm",
                                        assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".DepartmentForm"), LifestyleType.Transient);

            //数据字典档案
            IOC.Container.AddComponentLifeStyle("DataDictionaryForm",
                                        assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".DataDictionaryForm"), LifestyleType.Transient);
            IOC.Container.AddComponentLifeStyle("DataDictionaryForms",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".DataDictionaryForms"), LifestyleType.Transient);

            //科目余额档案
            IOC.Container.AddComponentLifeStyle("SubjectBalanceForm",
                                        assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".SubjectBalanceForm"), LifestyleType.Transient);

            
            //科目档案
            IOC.Container.AddComponentLifeStyle("SubjectForm",
                                        assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".SubjectForm"), LifestyleType.Transient);
            
            //公司档案
            IOC.Container.AddComponentLifeStyle("FirmForm",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".FirmForm"), LifestyleType.Transient);
            //项目管理
            IOC.Container.AddComponentLifeStyle("ProjectForm",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".ProjectForm"), LifestyleType.Transient);

            //供应商管理--主form
            IOC.Container.AddComponentLifeStyle("SupplierMainForm",
                                assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Procurement + ".SupplierMainForm"), LifestyleType.Transient);

            //供应商管理--新增编辑form

            //职员            
            IOC.Container.AddComponentLifeStyle("EmployeeForm",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Common + ".EmployeeForm"), LifestyleType.Transient);

            //仓库档案
            IOC.Container.AddComponentLifeStyle("WarehouseForm",
                                       assembly.GetType(Constant.UIRelectionConstant.UIAssembly + Constant.UIRelectionConstant.UICategory + Constant.UIRelectionConstant.Products + ".WarehouseForm"), LifestyleType.Transient);

        }
        private static void ConfigProxyOrRemotingIOC()
        {
            Assembly interfaceAssembly = ReflectUtil.ObtainAssembly(Constant.UIRelectionConstant.InterfaceAssembly);
            
            Assembly sererServiceAssembly = ReflectUtil.ObtainAssembly(Constant.UIRelectionConstant.ServerAssembly );
            if (FrameworkConfig.CallingBy ==Enums.CallingBy.Direct)
            {
                //CurrencyCategory
                IOC.Container.AddComponent("CurrencyCategory.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                  Constant.UIRelectionConstant.Common + ".ICurrencyCategoryService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                  Constant.UIRelectionConstant.Common + ".CurrencyCategoryService"));
                
                IOC.Container.AddComponent("CodeRulesService.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                  Constant.UIRelectionConstant.Common + ".ICodeRulesService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                  Constant.UIRelectionConstant.Common + ".CodeRulesService"));
                IOC.Container.AddComponent("ProjectsService.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                  Constant.UIRelectionConstant.Common + ".IProjectsService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                  Constant.UIRelectionConstant.Common + ".ProjectsService"));
                //数据字典
                IOC.Container.AddComponent("DataDictionaryService.Service",
                                          interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                 Constant.UIRelectionConstant.Common + ".IDataDictionaryService"),
                                          sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                 Constant.UIRelectionConstant.Common + ".DataDictionaryService"));

                IOC.Container.AddComponent("SubjectsService.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                  Constant.UIRelectionConstant.Common + ".ISubjectsService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                  Constant.UIRelectionConstant.Common + ".SubjectsService"));
                //部门档案
                IOC.Container.AddComponent("DepartmentsService.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                     Constant.UIRelectionConstant.Common + ".IDepartmentsService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                        Constant.UIRelectionConstant.Common + ".DepartmentsService"));
                //职员
                IOC.Container.AddComponent("EmployeesService.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                     Constant.UIRelectionConstant.Common + ".IEmployeesService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                        Constant.UIRelectionConstant.Common + ".EmployeesService"));

                //公司
                IOC.Container.AddComponent("FirmsService.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                     Constant.UIRelectionConstant.Common + ".IFirmsService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                        Constant.UIRelectionConstant.Common + ".FirmsService"));
                //仓库档案
                IOC.Container.AddComponent("WarehousesService.Service",
                                           interfaceAssembly.GetType(Constant.UIRelectionConstant.InterfaceAssembly +
                                                                     Constant.UIRelectionConstant.Products + ".IWarehousesService"),
                                           sererServiceAssembly.GetType(Constant.UIRelectionConstant.ServerAssembly + Constant.UIRelectionConstant.ServerCategory +
                                                                        Constant.UIRelectionConstant.Products + ".WarehousesService"));

                                                                       
            }
            else if (FrameworkConfig.CallingBy ==Enums.CallingBy.Remoting)
            {
                
            }
        }
        private static void ConfigRepositoryIOC()
        {
            
        }
    }
}
